<template>
	<view class="shareIndex">
		<!-- textarea区域 -->
		<u-input v-model="value" type="textarea" border height="300" placeholder="添加描述..." />
		<!-- 图片上传 -->
		<view class="uploadBox">
			<u-upload ref="uUpload" :action="action" :auto-upload="true" max-count="9"></u-upload>
		</view>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// textarea的输入内容
				value: '',
				actions: 'http://ts.lagou.uieee.com/api/v2/files',
				// 存储当前图片的ID值
				arr:[]
			};
		},
		methods: {
			async submit() {
				if (this.value.length === 0) {
					uni.showToast({
						title: '请填写描述信息',
						icon: 'none'
					})
					return false
				}
				if (this.$refs.uUpload.lists.length === 0) {
					uni.showToast({
						title: '请选择图片进行上传',
						icon: 'none'
					})
					return false
				}
				uni.showToast({
					title:'正在发布中',
					duration:2000
				})
				// 存储图片列表
				let picLists = this.$refs.uUpload.lists
				let picArr = []
				// 将遍历上传的图片进行一个集合
				picLists.map(async item => {
					picArr.push(await this.$u.api.uploadFile(item))
					let imgArr = []
					picArr.map(async item=>{
						imgArr.push({
							id:item.id,
						})
						if(imgArr.length === picLists.length){
							imgArr = [...imgArr]
							console.log(imgArr)
							let res = await this.$u.api.createFeed({
								feed_content: this.value,
								feed_from: 5,
								feed_mark: new Date().getTime(),
								images:imgArr
							})
							console.log(res)
							if (res.statusCode === 201 && res.data){
								uni.hideToast()
								uni.showToast({
									title:res.data.message
								})
								// 触发首页/我的页面监听函数
								uni.$emit('userIndexData')
								uni.$emit('getMineFeedList')
							}
						}
						
					})
				})
				uni.switchTab({
					url:'/pages/index/index',
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shareIndex {
		margin: 0 50upx;
		height: 100vh;

		::v-deep.u-input {
			margin: 20upx 0;
			width: 630upx;
		}

		.uploadBox {
			width: 700upx;
		}

		::v-deep.u-size-default {
			position: fixed;
			width: 630upx;
			left: 50upx;
			bottom: 20upx;
		}

	}
</style>
